package cn.cvs.controller;



import cn.cvs.pojo.SysUser;
import cn.cvs.service.user.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class HelloController {
    @Resource
    private SysUserService sysUserService;

    /**
     * 登录
     */
    @RequestMapping("/login")
    public String doLogin(String account, String password, HttpSession session, HttpServletRequest request) throws Exception {
        //核对用户信息
        SysUser sysUser = sysUserService.checkUser(account, password);
        //登录
        if (sysUser != null) {
            //将用户信息存入Session中
            session.setAttribute("userSession", sysUser);
            return "frame";
        } else {
            //转发输出错误信息
            request.setAttribute("error", "用户名或密码错误!");
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logOut(HttpSession session) {
        session.invalidate();
        return "login";
    }
}
